package com.tw.wechart2.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import com.tw.wechart2.R;
import com.tw.wechart2.bean.Tweet;
import com.tw.wechart2.constant.Constant;
import com.tw.wechart2.util.GlideUtil;
import com.tw.wechart2.util.MyLogUtils;

import java.util.List;
/**
 * @Description: tweet内容展示adapter
 * @Author: lanteng 2020/9/16
 * @Version: 1.0
 */
public class TweetsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private String TAG = TweetsAdapter.class.getSimpleName();
    private Context context;
    // tweet列表
    private List<Tweet> listTweets;

    // 顶部用户信息
    private int TYPE_TITLE = 0;
    // 中间tweets
    private int TYPE_TWEETS = 1;
    // 底部加载更多
    private int TYPE_FOOTER = 2;
    // 初始显示个数
    private int displayCount = 6;

    public TweetsAdapter(Context context, List<Tweet> listTweets, int displayCount) {
        this.context = context;
        this.listTweets = listTweets;
        this.displayCount = displayCount;
    }

    /**
     * @Description: 返回当前显示条数
     * @Author: lanteng 2020/9/21
     * @return
     */
    public int getDataSize() {
        return displayCount;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_TITLE) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_title, parent, false);
            return new TitleViewHolder(view);
        } else if (viewType == TYPE_TWEETS) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_tweet, parent, false);
            return new TweetViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_footer, parent, false);
            return new FooterViewHolder(view);
        }
        MyLogUtils.instance().logE(TAG, viewType + "");
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (null == holder) {
            return;
        }

        if (holder instanceof TitleViewHolder) {
            // 顶部title
            Tweet tweet = listTweets.get(position);
            TitleViewHolder holder0 = (TitleViewHolder) holder;
            GlideUtil.instance().displayImgage(context, tweet.getBigHead(), holder0.iv_big_head);
            GlideUtil.instance().displayImgage(context, tweet.getSmallHead(), holder0.iv_small_head);
            holder0.tv_user_name.setText(tweet.getUserName());
        } else if (holder instanceof TweetViewHolder) {
            // 中间tweets
            Tweet tweet = listTweets.get(position);

            final TweetViewHolder holder1 = (TweetViewHolder) holder;
            // tweets
            // 显示发送人头像
            GlideUtil.instance().displayImgage(context, tweet.getSenderAvatar(), holder1.iv_sender_img);
            // 显示发送人昵称
            holder1.tv_send_user_name.setText(tweet.getSenderNick());
            // 显示发送人发送内容
            if (null != tweet.getContent()) {
                holder1.tv_content.setVisibility(View.VISIBLE);
                holder1.tv_content.setText(tweet.getContent());
            } else {
                holder1.tv_content.setVisibility(View.GONE);
            }

            // 显示图片
            if (null != tweet.getListImages()) {
                holder1.rv_images.setVisibility(View.VISIBLE);
                GridLayoutManager manager = new GridLayoutManager(context, 3, GridLayoutManager.VERTICAL, false);
                holder1.rv_images.setLayoutManager(manager);

                ImageAdapter adapter = new ImageAdapter(context, tweet.getListImages());
                holder1.rv_images.setAdapter(adapter);
            } else {
                holder1.rv_images.setVisibility(View.GONE);
            }

            // 显示评论
            if (null != tweet.getListComment()) {
                holder1.rv_comments.setVisibility(View.VISIBLE);
                LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
                holder1.rv_comments.setLayoutManager(manager);

                CommonAdapter adapter = new CommonAdapter(context, tweet.getListComment());
                holder1.rv_comments.setAdapter(adapter);
            } else {
                holder1.rv_comments.setVisibility(View.GONE);
            }
        } else if (holder instanceof FooterViewHolder) {
            // 底部加载更多
        }
    }

    @Override
    public int getItemCount() {
        return (null == listTweets ? 0 : (listTweets.size() > displayCount ? (displayCount+1) : listTweets.size()));
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TITLE;
        } if (position == displayCount) {
            return TYPE_FOOTER;
        } else {
            return TYPE_TWEETS;
        }
    }

    public void initMore() {
        if (displayCount < listTweets.size()) {
            displayCount += 5;
            notifyDataSetChanged();
        }
    }

    /**
     * @Description: title Holder
     * @Author: lanteng 2020/9/21
     * @Version: 1.0
     */
    class TitleViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_big_head;
        private ImageView iv_small_head;
        private TextView tv_user_name;

        public TitleViewHolder(View itemView) {
            super(itemView);
            iv_big_head = itemView.findViewById(R.id.iv_big_head);
            iv_small_head = itemView.findViewById(R.id.iv_small_head);
            tv_user_name = itemView.findViewById(R.id.tv_user_name);
        }
    }

    /**
     * @Description: tweet Holder
     * @Author: lanteng 2020/9/21
     * @Version: 1.0
     */
    class TweetViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_sender_img;
        private TextView tv_send_user_name;
        private TextView tv_content;
        // 图片
        private RecyclerView rv_images;
        // 评论
        private RecyclerView rv_comments;

        public TweetViewHolder(View itemView) {
            super(itemView);
            iv_sender_img = itemView.findViewById(R.id.iv_sender_img);
            tv_send_user_name = itemView.findViewById(R.id.tv_send_user_name);
            tv_content = itemView.findViewById(R.id.tv_content);
            rv_images = itemView.findViewById(R.id.rv_images);
            rv_comments = itemView.findViewById(R.id.rv_comments);
        }
    }

    /**
     * @Description: footer Holder
     * @Author: lanteng 2020/9/21
     * @return
     */
    class FooterViewHolder extends RecyclerView.ViewHolder {

        public FooterViewHolder(View itemView) {
            super(itemView);
        }
    }

}
